package com.hxd.otc.utils;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.hxd.otc.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by LZY on 2017/7/12.
 * <p>
 * 日期工具类，提供格式化日期，解析字符串日期为时间
 */

public class DateUtil {
    public static final String YY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YY_MM = "yyyy-MM";
    public static final String MM = "MM月";
    public static final String YY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";

    public static String format(long time, String formatStr) {
        if (TextUtils.isEmpty(formatStr)) return "";
        return format(new Date(time), formatStr);
    }

    public static String format(Date date, String formatStr) {
        if (date == null) return "";
        return new SimpleDateFormat(formatStr, Locale.getDefault()).format(date);
    }

    public static Date parseToDate(String time, String dateFormat) {
        if (TextUtils.isEmpty(time) || TextUtils.isEmpty(dateFormat)) return null;

        try {
            return new SimpleDateFormat(dateFormat, Locale.getDefault()).parse(time);
        } catch (ParseException e) {
            return null;
        }
    }

    public static long parseToStamp(String time, String dataFormat) {
        Date date = parseToDate(time, dataFormat);
        if (date == null) return 0;
        return date.getTime();
    }

    /**
     * 获取时间差
     *
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param dataFormat
     * @return
     */
    public static long getTimeSubtraction(String startTime, String endTime, String dataFormat) {
        Date date1 = parseToDate(startTime, dataFormat);
        Date date2 = parseToDate(endTime, dataFormat);

        if (date1 == null) return 0;
        if (date2 == null) return 0;

        return date2.getTime() - date1.getTime();
    }

    /**
     * 获取未付款倒计时
     *
     * @param currentTime 订单创建时间
     * @param createTime  后台返回系统时间
     * @param dataFormat
     * @return
     */
    public static long getunPayCountdown(String currentTime, String createTime,long time, String dataFormat) {
        Date date1 = parseToDate(currentTime, dataFormat);
        Date date2 = parseToDate(createTime, dataFormat);

        if (date1 == null) return 0;
        if (date2 == null) return 0;

        return date2.getTime() - date1.getTime() + (time * 60 * 1000);
    }

    /**
     * 获取已付款倒计时
     *
     * @param currentTime 订单创建时间
     * @param createTime  后台返回系统时间
     * @param dataFormat
     * @return
     */
    public static long getPayedCountdown(String currentTime, String createTime, String dataFormat) {
        Date date1 = parseToDate(currentTime, dataFormat);
        Date date2 = parseToDate(createTime, dataFormat);

        if (date1 == null) return 0;
        if (date2 == null) return 0;

        return date2.getTime() - date1.getTime() + (24 * 60 * 60 * 1000);
    }

    /**
     * 将秒转成 分：秒 格式
     *
     * @param timeInMills
     * @return
     */
    @NonNull
    public static String getTimeMinutesSecond(long timeInMills) {
        String strminutes = "0";
        String strsecond = "0";

        long minutes = (timeInMills) / (60);
        long second = (timeInMills - minutes * (60));

//        LogUtils.i(minutes + "分" + second + "秒");

        if (minutes < 10) {
            strminutes = 0 + "" + minutes;
        } else {
            strminutes = minutes + "";
        }
        if (second < 10) {
            strsecond = 0 + "" + second;
        } else {
            strsecond = second + "";
        }
        return UIUtils.getContext().getResources().getString(R.string.count_down_second, strminutes, strsecond);
    }

    /**
     * 格式化消息界面时间
     *
     * @param source
     * @return
     */
    public static String formatMessageTime(String source) {
        if (TextUtils.isEmpty(source)) return source;

        Date sourceDate = parseToDate(source, YY_MM_DD_HH_MM_SS);
        Calendar calendar = Calendar.getInstance();

        String today = format(calendar.getTime(), "yyyy-MM-dd");
        if (source.contains(today)) {
            return format(sourceDate, "HH:mm");
        }
        calendar.add(Calendar.DATE, -1);

        String yesterday = format(calendar.getTime(), "yyyy-MM-dd");
        if (source.contains(yesterday)) {
            return "昨天";
        }

        int year = calendar.get(Calendar.YEAR);

        calendar.setTime(sourceDate);
        int sourceYear = calendar.get(Calendar.YEAR);
        if (year == sourceYear) {
            return format(sourceDate, "MM月dd日");
        } else {
            return format(sourceDate, "yyyy年MM月dd日");
        }
    }

}
